home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.02 Feb 94 / DigiSign / SoundByteUtils.c < prev    next >
Encoding:
Text File  |  1993-12-03  |  5.7 KB  |  275 lines  |  [TEXT/KAHL]

  1. // Contains utilities, add this and SoundByte.c to a project
  2. // along with MacTraps lib in Think C or compiled with the
  3. // usual C libraries in MPW.  This has not been compiled
  4. // under MPW but you should have very little problems if any
  5. // doing so.
  6.  
  7. #include <Quickdraw.h>
  8. #include <Fonts.h>
  9. #include <Windows.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include <Dialogs.h>
  13. #include <Memory.h>
  14. #include <Menus.h>
  15. #include <Traps.h>
  16. #include <GestaltEqu.h>
  17. #include <AppleEvents.h>
  18. #include <Sound.h>
  19.  
  20. #include "OCEErrors.h"
  21. #include "DigitalSignature.h"    
  22.  
  23. #include "SoundByte.h"
  24. #include "SoundByteUtils.h"
  25.  
  26. extern Environment gEnvironment;
  27.  
  28. void BasicInitializations()
  29. {
  30.     InitGraf(&qd.thePort);
  31.     InitFonts();
  32.     InitWindows();
  33.     InitMenus();
  34.     TEInit();
  35.     InitDialogs(nil);
  36.     InitCursor();
  37. }
  38.  
  39. Boolean Prerequisites()
  40. {
  41.     OSErr    error;
  42.     long    value;
  43.     Boolean    result = false;
  44.     
  45.     do {
  46.         if (NGetTrapAddress(_GestaltDispatch,ToolTrap) == NGetTrapAddress(_Unimplemented,ToolTrap))
  47.             break;
  48.         if (error = Gestalt(gestaltDigitalSignatureVersion, &value))
  49.             break;
  50.         result = true;
  51.         } while (0);
  52.     
  53.     return result;
  54. }
  55.  
  56. void InitSoundByteMenus()
  57. {
  58.     Handle    menuBar = GetNewMBar(rMenuBar);
  59.     
  60.     if (menuBar == nil) 
  61.         AlertOSErr(ResError());
  62.         
  63.     SetMenuBar(menuBar);
  64.     ReleaseResource(menuBar);
  65.  
  66.     AddResMenu(GetMHandle(mApple), 'DRVR');
  67.     DrawMenuBar();
  68. }
  69.  
  70. OSErr GotRequiredAEParams(AppleEvent *event)
  71. {
  72.     DescType    returnedType;
  73.     Size        actualSize;
  74.     OSErr        error;
  75.     
  76.     error = AEGetAttributePtr(event, keyMissedKeywordAttr, typeWildCard, &returnedType, nil, 0, &actualSize);
  77.     if (error == errAEDescNotFound)
  78.         return noErr;
  79.     else if (error == noErr)
  80.         return errAEEventNotHandled;
  81.     else
  82.         return error;
  83. }
  84.  
  85. pascal OSErr DoAEOpenApp(AppleEvent *event, AppleEvent *reply, long refCon)
  86. {
  87.     #pragma unused (reply, refCon)
  88.     return GotRequiredAEParams(event);
  89. }
  90.  
  91. pascal OSErr DoAEQuitApp(AppleEvent *event, AppleEvent *reply, long refCon)
  92. {
  93.     #pragma unused (reply, refCon)
  94.     return GotRequiredAEParams(event);
  95. }
  96.  
  97. pascal OSErr DoAEPrintDoc(AppleEvent *event, AppleEvent *reply, long refCon)
  98. {
  99.     #pragma unused (reply, refCon)
  100.     return GotRequiredAEParams(event);
  101. }
  102.  
  103. pascal OSErr DoAEOpenDoc(AppleEvent *event, AppleEvent *reply, long refCon)
  104. {
  105.     #pragma unused (reply, refCon)
  106.  
  107.     long            index;            
  108.     AEDescList        docList;
  109.     Size            actualSize;
  110.     AEKeyword        keyword;
  111.     DescType        type;
  112.     FSSpec            theFile;
  113.     long            count;
  114.     OSErr            error;
  115.  
  116.     do {
  117.         if (error = AEGetParamDesc(event, keyDirectObject, typeAEList, &docList))    // get the doc list
  118.             break;
  119.         if (error = GotRequiredAEParams(event))
  120.             break;
  121.         if (error = AECountItems(&docList, &count))    
  122.             break;
  123.         for (index=1; index<=count; index++)
  124.             {
  125.             if (error = AEGetNthPtr(&docList, index, typeFSS, &keyword, &type, (Ptr)&theFile, sizeof(FSSpec), &actualSize))
  126.                 break;
  127.             }
  128.         } while (0);
  129.         
  130.     AEDisposeDesc(&docList);
  131.  
  132.     AlertOSErr(error);
  133.  
  134.     return error;
  135. }
  136.  
  137. void InitAppleEvents()
  138. {
  139.     OSErr    error;
  140.     
  141.     do {
  142.         if (error = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, (EventHandlerProcPtr)DoAEOpenApp, 0, false))
  143.             break;
  144.         if (error = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, (EventHandlerProcPtr)DoAEOpenDoc, 0, false))
  145.             break;
  146.         if (error = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, (EventHandlerProcPtr)DoAEPrintDoc, 0, false))
  147.             break;
  148.         if (error = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (EventHandlerProcPtr)DoAEQuitApp, 0, false))
  149.             break;
  150.         } while (0);
  151.         
  152.     AlertOSErr(error);
  153. }
  154.  
  155. void ApplicationStartup()
  156. {
  157.     long    soundAttrs;
  158.     
  159.     Gestalt(gestaltSoundAttr, &soundAttrs);
  160.     
  161.     gEnvironment.hasSoundInput     = (soundAttrs & gestaltHasSoundInputDevice);
  162.     gEnvironment.running         = true;
  163.     gEnvironment.inBackground    = false;
  164.     gEnvironment.statusDialog     = nil;
  165.     
  166.     InitSoundByteMenus();
  167.     InitAppleEvents();
  168. }
  169.  
  170. Handle GetTempHandle()
  171. {
  172.     OSErr    ignore;
  173.     Size    max;
  174.     Size    canFree = MFFreeMem();
  175.     
  176.     max = MFMaxMem(&canFree) / 2;    // don't be too greedy
  177.     return TempNewHandle(max, &ignore);
  178. }
  179.  
  180. pascal Boolean AboutFilter(DialogPtr dialog, EventRecord *event, short *item)
  181. {
  182.     Handle aboutSound;
  183.     
  184.     if (event->what == updateEvt)
  185.         {
  186.         DrawDialog(dialog);
  187.         aboutSound = Get1NamedResource('snd ', "\pPablo's Bass");
  188.         if (aboutSound)
  189.             {
  190.             SetCursor(*GetCursor(watchCursor));
  191.             SndPlay(nil, aboutSound, false);
  192.             SetCursor(&qd.arrow);
  193.             }
  194.         }
  195.     
  196.     return false;
  197. }
  198.  
  199. void DoAbout()
  200. {
  201.     short         ignore;
  202.     DialogPtr    dialog         = GetNewDialog(rSoundByteAboutDialog, nil, kMoveToFront);
  203.         
  204.     ModalDialog(AboutFilter, &ignore);
  205.             
  206.     DisposeDialog(dialog);
  207. }
  208.  
  209. void DisplayUserAlert(short messageID, Boolean exitToShell, OSErr error)
  210. {
  211.     short    itemHit;
  212.     Str255    message;
  213.     Str255    errorString;
  214.  
  215.     if (error != noErr)        // just a notification really
  216.         NumToString(error, errorString);
  217.     else
  218.         errorString[0] = 0;
  219.         
  220.     GetIndString(message, rMessages, messageID);
  221.     ParamText(message, errorString, kNullPString, kNullPString);
  222.  
  223.     SysBeep(1);
  224.     itemHit = NoteAlert(rSimpleAlert, nil);
  225.     
  226.     if (exitToShell) 
  227.         ExitToShell();
  228. }
  229.  
  230. void AlertOSErr(OSErr error)
  231. {
  232.     Boolean bail         = true;
  233.     short     messageID    = kGenericError;
  234.     
  235.     switch (error)
  236.         {
  237.         case noErr:
  238.         case userCanceledErr:
  239.             return;
  240.         case memFullErr:
  241.             messageID = kNoMemory;
  242.             break;    
  243.         case paramErr:
  244.             messageID = kBadParam;
  245.             break;
  246.         case errAECoercionFail:
  247.         case errAENotAEDesc:
  248.         case errAEDescNotFound:
  249.             messageID = kAppleEvents;
  250.             break;
  251.         case resNotFound:
  252.             messageID = kNeededResourceMissing;
  253.             break;
  254.         case kSIGVerifyFailedErr:
  255.             bail = false;
  256.             messageID = kVerifyFailed;
  257.             break;    
  258.         case kSIGPasswordErr:
  259.             bail = false;
  260.             messageID = kPasswordFailed;
  261.             break;    
  262.         case kSIGSignerErr:
  263.             bail = false;
  264.             messageID = kSignerProblem;
  265.             break;
  266.         case kSIGSignerNotValidErr:
  267.             bail = false;
  268.             messageID = kSignerNotValid;
  269.             break;
  270.         }
  271.     
  272.     DisplayUserAlert(messageID, bail, error);    
  273. }
  274.  
  275.